window.onload = function() {
	init();
}

function init() {
	globals.avoidCache = {
		"base":		(new Date).getTime(),
		"dif":		0
	};

	globals.color = {
		"piasmeno":	'#990000',
		"error":	'#FF0000',
		"ok":		'#000000'
	};

	globals.duration = {
		"fyi":		5000
	};

	globals.timeDif -= parseInt((new Date).getTime() / 1000);
	globals.paraskinio.list = [
		'neutral.jpg',
		'feoprasino.jpg',
		'indian.jpg',
		'teal.jpg',
		'lila.jpg',
		'tsoxa.jpg',
		'xartoni.jpg',
		'ble.jpg',
		'bleVathi.jpg',
		'griSkouro.jpg',
		'kafe.jpg',
		'kafeAnikto.jpg',
		'kafeKokino.jpg',
		'kokino.jpg',
		'kokinoVathi.jpg',
		'mavro.jpg',
		'moustardi.jpg',
		'prasino.jpg',
		'prasinoSkouro.jpg',
		'xaki.jpg',
		'plakaki.jpg',
		'tetragona.jpg',
		'kare.gif'
	];
	if (notSet(globals.paraskinio.index)) { globals.paraskinio.index = 0; }
	document.body.style.backgroundImage = 'url(' + globals.server +
		'images/background/' + globals.paraskinio.list[globals.paraskinio.index] + ')';
}

function Request(page, asynch) {
	this.xhr = new XMLHttpRequest();
	if (notSet(this.xhr)) {
		this.xhr = new ActiveXObject("Msxml2.XMLHTTP");
		if (notSet(this.xhr)) {
			this.xhr = new ActiveXObject("Microsoft.XMLHTTP");
			if (notSet(this.xhr)) {
				location.href = globals.server + 'error.php?minima=' +
					uri('new Request: failed');
				return;
			}
		}
	}

	if (notSet(asynch)) {
		asynch = true;
	}

	this.page = page;
	this.xhr.open('POST', globals.server + page + '.php', asynch);

	this.send = function(data) {
		if (notSet(this.xhr)) {
			return 'undefined Ajax request';
		}

		if (notSet(data)) { data = ''; }
		else if (data != '') { data += '&'; }
		data += 'timeDif=' + globals.timeDif;
		data += '&avoidCache=' + globals.avoidCache.base + ':' + globals.avoidCache.dif++;

		this.xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		this.xhr.send(data);
	};

	this.getResponse = function() {
		if (notSet(this.xhr)) {
			return 'undefined Ajax request';
		}

		if (notSet(this.xhr.status)) {
			return 'undefined Ajax returned status';
		}

		if (this.xhr.status != 200) {
			return this.page + ' (status = ' + this.xhr.status + ')';
		}

		if (notSet(this.xhr.responseText)) {
			return this.page + ': undefined Ajax response';
		}

		return this.xhr.responseText;
	};
}

function uri(s) {
	return(encodeURIComponent(s));
}

function fyi(msg, fyiId, dur) {
	if (notSet(fyiId)) { fyiId = 'fyiTop'; }
	var fyi = getelid(fyiId);
	if (notSet(fyi)) { return; }

	if (isSet(fyi.clearTimer)) {
		clearTimeout(fyi.clearTimer);
	}

	if (notSet(msg) || (msg === '')) {
		var visibility = 'hidden';
		msg = '&#8203;';
	}
	else {
		visibility = 'visible';
	}

	if (notSet(dur)) {
		dur = globals.duration.fyi;
	}

	fyi.innerHTML = msg;
	fyi.style.visibility = visibility;
	if (dur > 0) {
		fyi.clearTimer = setTimeout(function() {
			clearFyi(fyi);
		}, dur);
	}
}

function clearFyi(fyi) {
	try {
		fyi.innerHTML = '&nbsp;';
		fyi.style.visibility = 'hidden';
	} catch(e) {};
}

function fyiError(msg, fyiId, dur) {
	fyi('<div style="padding-left: 0.2em; text-align: left; color: ' +
		globals.color.error + ';">' + msg + '</div>', fyiId, dur);
}

function clearError(fld, fyiId) {
	if (isSet(fld)) { fld.style.color = globals.color.ok; }
	fyi(null, fyiId);
}

function validEmail(email) {
	if (notSet(email)) { return false; }
	return email.match(/^[a-zA-Z0-9_\.-]+\@([a-zA-Z0-9-]+\.)+([a-zA-Z0-9]{2,4})+$/);
}

function isSet(x) {
	if (x === undefined) { return false; }
	if (x === null) { return false; }
	return true;
}

function notSet(x) {
	return(!isSet(x));
}

function getelid(id) {
	return(document.getElementById(id));
}

String.prototype.trim = function() {
	return (this.replace(/^\s+|\s+$/g,""));
}

String.prototype.ltrim = function() {
	return (this.replace(/^\s+/,""));
}

String.prototype.rtrim = function() {
	return (this.replace(/\s+$/,""));
}

function apolitaXY(obj) {
	var XY = {
		x: 0,
		y: 0
	};

	while (obj.offsetParent) {
		XY.x += obj.offsetLeft;
		XY.y += obj.offsetTop;
		obj = obj.offsetParent;
	}

	return(XY);
}

function svisePeriexomeno(p) {
	if (!p.hasChildNodes()) { return; }
	while (p.childNodes.length > 0) {
		p.removeChild(p.firstChild);       
	} 
}

function isInternetExplorer() {
	if (notSet(navigator)) { return false; }
	if (notSet(navigator.appName)) { return false; }
	if (notSet(navigator.userAgent)) { return false; }
	if (navigator.appName != 'Microsoft Internet Explorer') { return false; }

	var rv = -1;
	var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	if (isSet(re.exec(navigator.userAgent))) { rv = parseFloat(RegExp.$1); }
	return (rv > -1);
}
